<template>
  <el-dialog
    :title="!dataForm.mhId ? '质量录入' : '质量录入'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="黄度" prop="mhHd">
            <el-input v-model="dataForm.mhHd" placeholder="黄度"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="衣指" prop="mhYz">
            <el-input v-model="dataForm.mhYz" placeholder="衣指(个)"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单铃重" prop="mhDlz">
            <el-input v-model="dataForm.mhDlz" placeholder="单铃重(g)"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="籽指" prop="mhZz">
            <el-input v-model="dataForm.mhZz" placeholder="籽指(个)"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="6">
          <el-form-item label="籽棉产量" prop="mhZmcl">
            <el-input v-model="dataForm.mhZmcl" placeholder="籽棉产量(kg)"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="马克隆值" prop="mhMklz">
            <el-input v-model="dataForm.mhMklz" placeholder="马克隆值"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="整齐度" prop="mhZqd">
            <el-input v-model="dataForm.mhZqd" placeholder="整齐度(%)"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="比强度" prop="mhBqd">
            <el-input v-model="dataForm.mhBqd" placeholder="比强度(CN/tex)"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="伸长率" prop="mhSzl">
            <el-input v-model="dataForm.mhSzl" placeholder="伸长率(%)"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="反射率" prop="mhFsl">
            <el-input v-model="dataForm.mhFsl" placeholder="反射率(%)"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="衣分" prop="mhYf" >
            <el-input v-model="dataForm.mhYf" placeholder="衣分(%)" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label="棉花编号" prop="mhBh" v-if="dataForm.mhId">
          <el-input v-model="dataForm.mhBh" placeholder="棉花编号" disabled></el-input>
        </el-form-item>
        </el-col>
      </el-row>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        mhId: 0,
        mhBh: '',
        recordDate: '',
        mhNumber: '',
        mhHeight: '',
        mhJc: '',
        mhYps: '',
        mhDygzjgd: '',
        mhGzs: '',
        mhLs: '',
        mhHs: '',
        mhZls: '',
        mhDzjls: '',
        mhTxs: '',
        mhGzsj: '',
        mhGzts: '',
        mhLc: '',
        mhLk: '',
        mhLx: '',
        mhDlz: '',
        mhYf: '',
        mhZmcl: '',
        mhMklz: '',
        mhZqd: '',
        mhBqd: '',
        mhSzl: '',
        mhFsl: '',
        mhHd: '',
        mhYz: '',
        mhZz: '',
        addUser: ''
      },
      dataRule: {
        mhBh: [
          {required: true, message: '棉花植株的唯一标识,唯一编号，用于区分不同植株的生长记录不能为空', trigger: 'blur'}
        ],
        recordDate: [
          {required: true, message: '记录日期，精确到天不能为空', trigger: 'blur'}
        ],
        mhNumber: [
          {required: true, message: '总株数（如果死亡-1）不能为空', trigger: 'blur'}
        ],
        mhHeight: [
          {required: true, message: '株高不能为空', trigger: 'blur'}
        ],
        mhJc: [
          {required: true, message: '茎粗不能为空', trigger: 'blur'}
        ],
        mhYps: [
          {required: true, message: '叶片数不能为空', trigger: 'blur'}
        ],
        mhDygzjgd: [
          {required: true, message: '第一果枝节高度不能为空', trigger: 'blur'}
        ],
        mhGzs: [
          {required: true, message: '果枝数不能为空', trigger: 'blur'}
        ],
        mhLs: [
          {required: true, message: '蕾数不能为空', trigger: 'blur'}
        ],
        mhHs: [
          {required: true, message: '花数不能为空', trigger: 'blur'}
        ],
        mhZls: [
          {required: true, message: '总铃数不能为空', trigger: 'blur'}
        ],
        mhDzjls: [
          {required: true, message: '单株结铃数不能为空', trigger: 'blur'}
        ],
        mhTxs: [
          {required: true, message: '吐絮数不能为空', trigger: 'blur'}
        ],
        mhGzsj: [
          {required: true, message: '果枝始节不能为空', trigger: 'blur'}
        ],
        mhGzts: [
          {required: true, message: '果枝台数不能为空', trigger: 'blur'}
        ],
        mhLc: [
          {required: true, message: '铃长不能为空', trigger: 'blur'}
        ],
        mhLk: [
          {required: true, message: '铃宽不能为空', trigger: 'blur'}
        ],
        mhLx: [
          {required: true, message: '铃型不能为空', trigger: 'blur'}
        ],
        mhDlz: [
          {required: true, message: '单铃重（克）不能为空', trigger: 'blur'}
        ],
        mhYf: [
          {required: true, message: '衣分（%）不能为空', trigger: 'blur'}
        ],
        mhZmcl: [
          {required: true, message: '籽棉产量不能为空', trigger: 'blur'}
        ],
        mhMklz: [
          {required: true, message: '马克隆值不能为空', trigger: 'blur'}
        ],
        mhZqd: [
          {required: true, message: '整齐度不能为空', trigger: 'blur'}
        ],
        mhBqd: [
          {required: true, message: '比强度不能为空', trigger: 'blur'}
        ],
        mhSzl: [
          {required: true, message: '伸长率不能为空', trigger: 'blur'}
        ],
        mhFsl: [
          {required: true, message: '反射率不能为空', trigger: 'blur'}
        ],
        mhHd: [
          {required: true, message: '黄度不能为空', trigger: 'blur'}
        ],
        mhYz: [
          {required: true, message: '衣指(每 100 粒棉籽上纤维重量（衣指）)不能为空', trigger: 'blur'}
        ],
        mhZz: [
          {required: true, message: '籽指(指 100 粒棉籽的重量)不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.mhId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.mhId) {
          this.$http({
            url: this.$http.adornUrl(`/mh/growth/info/${this.dataForm.mhId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.mhBh = data.growth.mhBh
              this.dataForm.recordDate = data.growth.recordDate
              this.dataForm.mhNumber = data.growth.mhNumber
              this.dataForm.mhHeight = data.growth.mhHeight
              this.dataForm.mhJc = data.growth.mhJc
              this.dataForm.mhYps = data.growth.mhYps
              this.dataForm.mhDygzjgd = data.growth.mhDygzjgd
              this.dataForm.mhGzs = data.growth.mhGzs
              this.dataForm.mhLs = data.growth.mhLs
              this.dataForm.mhHs = data.growth.mhHs
              this.dataForm.mhZls = data.growth.mhZls
              this.dataForm.mhDzjls = data.growth.mhDzjls
              this.dataForm.mhTxs = data.growth.mhTxs
              this.dataForm.mhGzsj = data.growth.mhGzsj
              this.dataForm.mhGzts = data.growth.mhGzts
              this.dataForm.mhLc = data.growth.mhLc
              this.dataForm.mhLk = data.growth.mhLk
              this.dataForm.mhLx = data.growth.mhLx
              this.dataForm.mhDlz = data.growth.mhDlz
              this.dataForm.mhYf = data.growth.mhYf
              this.dataForm.mhZmcl = data.growth.mhZmcl
              this.dataForm.mhMklz = data.growth.mhMklz
              this.dataForm.mhZqd = data.growth.mhZqd
              this.dataForm.mhBqd = data.growth.mhBqd
              this.dataForm.mhSzl = data.growth.mhSzl
              this.dataForm.mhFsl = data.growth.mhFsl
              this.dataForm.mhHd = data.growth.mhHd
              this.dataForm.mhYz = data.growth.mhYz
              this.dataForm.mhZz = data.growth.mhZz
              this.dataForm.addUser = data.growth.addUser
            }
          })
        }
      })
    },
    //  获得当前时间
    getCurrentTime () {
      return new Date()
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/mh/growth/${!this.dataForm.mhId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'mhId': this.dataForm.mhId || undefined,
              'mhBh': this.dataForm.mhBh,
              'recordDate': this.getCurrentTime(),
              'mhNumber': this.dataForm.mhNumber,
              'mhHeight': this.dataForm.mhHeight,
              'mhJc': this.dataForm.mhJc,
              'mhYps': this.dataForm.mhYps,
              'mhDygzjgd': this.dataForm.mhDygzjgd,
              'mhGzs': this.dataForm.mhGzs,
              'mhLs': this.dataForm.mhLs,
              'mhHs': this.dataForm.mhHs,
              'mhZls': this.dataForm.mhZls,
              'mhDzjls': this.dataForm.mhDzjls,
              'mhTxs': this.dataForm.mhTxs,
              'mhGzsj': this.dataForm.mhGzsj,
              'mhGzts': this.dataForm.mhGzts,
              'mhLc': this.dataForm.mhLc,
              'mhLk': this.dataForm.mhLk,
              'mhLx': this.dataForm.mhLx,
              'mhDlz': this.dataForm.mhDlz,
              'mhYf': this.dataForm.mhYf,
              'mhZmcl': this.dataForm.mhZmcl,
              'mhMklz': this.dataForm.mhMklz,
              'mhZqd': this.dataForm.mhZqd,
              'mhBqd': this.dataForm.mhBqd,
              'mhSzl': this.dataForm.mhSzl,
              'mhFsl': this.dataForm.mhFsl,
              'mhHd': this.dataForm.mhHd,
              'mhYz': this.dataForm.mhYz,
              'mhZz': this.dataForm.mhZz,
              'addUser': this.$store.state.user.name
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

